package com.bsj.sipgateway.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.def.dto.AlarmQueryDTO;
import com.bsj.power.common.def.entity.log.VideoOpLog;
import com.bsj.power.common.def.enums.VideoOpTypeEnum;
import com.bsj.power.common.def.vo.video.log.VideoOpLogExcelVO;
import com.bsj.power.common.def.vo.video.log.VideoOpLogListVO;
import com.bsj.power.common.def.vo.video.log.VideoOpLogSearchVO;
import com.bsj.power.common.mapper.VideoOpLogMapper;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.common.util.StringUtils;
import com.bsj.sipgateway.web.service.IVideoOpLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : zhuojie.c
 * @version V1.0
 * @Description: TODO
 * @date Date : 2024年08月13日
 */
@Slf4j
@Service("videoOpLogServiceImpl")
public class VideoOpLogServiceImpl implements IVideoOpLogService {

    @Resource
    private VideoOpLogMapper videoOpLogMapper;

    @Override
    public JsonResult<List<VideoOpLogListVO>> videoOpLogList(VideoOpLogSearchVO videoOpLogSearchVO) {
        List<VideoOpLogListVO> videoOpLogListVOList = new ArrayList<>();
        IPage<VideoOpLog> videoOpLogPage = new Page<>(videoOpLogSearchVO.getPageNumber(), videoOpLogSearchVO.getPageSize());
        QueryWrapper<VideoOpLog> queryWrapper = new QueryWrapper<>();
        if (videoOpLogSearchVO.getOpType() != null && videoOpLogSearchVO.getOpType() != 0) {
            queryWrapper.eq("op_type", videoOpLogSearchVO.getOpType());
        } else {
            queryWrapper.notIn("op_type", List.of(VideoOpTypeEnum.DEVICE_LOGOUT_ABNORMAL.getOpType()
                    , VideoOpTypeEnum.CALL_HELP_ALARM.getOpType(), VideoOpTypeEnum.ELECTRON_PATH_ALARM.getOpType()));
        }
        if (StringUtils.isNotEmpty(videoOpLogSearchVO.getDeviceId())) {
            queryWrapper.eq("device_id", videoOpLogSearchVO.getDeviceId());
        }
        if (StringUtils.isNotEmpty(videoOpLogSearchVO.getOpUserName())) {
            queryWrapper.eq("op_user_name", videoOpLogSearchVO.getOpUserName());
        }
        if (StringUtils.isNotEmpty(videoOpLogSearchVO.getNickName())) {
            queryWrapper.like("nick_name", "%" + videoOpLogSearchVO.getNickName() + "%");
        }
        if (StringUtils.isNotEmpty(videoOpLogSearchVO.getEquipmentNumber())) {
            queryWrapper.like("equipment_number", "%" + videoOpLogSearchVO.getEquipmentNumber() + "%");
        }
        if (videoOpLogSearchVO.getStartTime() != null && videoOpLogSearchVO.getEndTime() != null) {
            queryWrapper.between("op_time", videoOpLogSearchVO.getStartTime(), videoOpLogSearchVO.getEndTime());
        }
        queryWrapper.orderByDesc("op_time");
        IPage<VideoOpLog> iPage = videoOpLogMapper.selectPage(videoOpLogPage, queryWrapper);
        List<VideoOpLog> videoOpLogList = iPage.getRecords();
        long total = iPage.getTotal();
        if (CollectionUtil.isNotEmpty(videoOpLogList)) {
            videoOpLogList.forEach(v -> {
                VideoOpLogListVO videoOpLogListVO = new VideoOpLogListVO();
                BeanUtils.copyProperties(v, videoOpLogListVO);
                videoOpLogListVOList.add(videoOpLogListVO);
            });
        }
        List<VideoOpLogListVO> collect = videoOpLogListVOList.stream().sorted(Comparator.comparing(VideoOpLogListVO::getCreateTime).reversed()).collect(Collectors.toList());
        return JsonResult.success(collect, total);
    }

    @Override
    public List<VideoOpLogExcelVO> exportVideoOpLogExcelVO(VideoOpLogSearchVO videoOpLogSearchVO) {
        List<VideoOpLogExcelVO> excelVOS = new ArrayList<>();
//        IPage<VideoOpLog> videoOpLogPage = new Page<>(videoOpLogSearchVO.getPageNumber(), videoOpLogSearchVO.getPageSize());
        QueryWrapper<VideoOpLog> queryWrapper = new QueryWrapper<>();
        if (videoOpLogSearchVO.getOpType() != null && videoOpLogSearchVO.getOpType() != 0) {
            queryWrapper.eq("op_type", videoOpLogSearchVO.getOpType());
        } else {
            queryWrapper.notIn("op_type", List.of(VideoOpTypeEnum.DEVICE_LOGOUT_ABNORMAL.getOpType()
                    , VideoOpTypeEnum.CALL_HELP_ALARM.getOpType(), VideoOpTypeEnum.ELECTRON_PATH_ALARM.getOpType()));
        }
        if (StringUtils.isNotEmpty(videoOpLogSearchVO.getDeviceId())) {
            queryWrapper.eq("device_id", videoOpLogSearchVO.getDeviceId());
        }
        if (StringUtils.isNotEmpty(videoOpLogSearchVO.getOpUserName())) {
            queryWrapper.eq("op_user_name", videoOpLogSearchVO.getOpUserName());
        }
        if (StringUtils.isNotEmpty(videoOpLogSearchVO.getNickName())) {
            queryWrapper.like("nick_name", "%" + videoOpLogSearchVO.getNickName() + "%");
        }
        if (StringUtils.isNotEmpty(videoOpLogSearchVO.getEquipmentNumber())) {
            queryWrapper.like("equipment_number", "%" + videoOpLogSearchVO.getEquipmentNumber() + "%");
        }
        if (videoOpLogSearchVO.getStartTime() != null && videoOpLogSearchVO.getEndTime() != null) {
            queryWrapper.between("op_time", videoOpLogSearchVO.getStartTime(), videoOpLogSearchVO.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
        List<VideoOpLog> videoOpLogList = videoOpLogMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(videoOpLogList)) {
            for (int i = 0; i < videoOpLogList.size(); i++) {
                VideoOpLog videoOpLog = videoOpLogList.get(i);
                VideoOpLogExcelVO excelVO = new VideoOpLogExcelVO();
                excelVO.setSerialNum(i + 1);
                excelVO.setDeviceId(videoOpLog.getDeviceId());
                excelVO.setNickName(videoOpLog.getNickName());
                excelVO.setOpUserName(videoOpLog.getOpUserName());
                excelVO.setName(videoOpLog.getName());
                excelVO.setContent(videoOpLog.getContent());
                excelVO.setOpTimeStr(DateUtils.dateToStr(videoOpLog.getOpTime()));
                excelVOS.add(excelVO);
            }
        }
        return excelVOS;
    }

    @Override
    public JsonResult<List<VideoOpLogListVO>> abnormalOfflineAlarmList(AlarmQueryDTO alarmQueryDTO) {
        List<VideoOpLogListVO> videoOpLogListVOList = new ArrayList<>();
        IPage<VideoOpLog> videoOpLogPage = new Page<>(alarmQueryDTO.getPageNumber(), alarmQueryDTO.getPageSize());
        QueryWrapper<VideoOpLog> queryWrapper = new QueryWrapper<>();
        Integer opType = alarmQueryDTO.getOpType();
        if (opType != null) {
            queryWrapper.eq("op_type", opType);
        } else {
            queryWrapper.in("op_type", VideoOpTypeEnum.DEVICE_LOGOUT_ABNORMAL.getOpType()
                    , VideoOpTypeEnum.CALL_HELP_ALARM.getOpType(), VideoOpTypeEnum.ELECTRON_PATH_ALARM.getOpType());
        }
        if (StringUtils.isNotEmpty(alarmQueryDTO.getDeviceId())) {
            queryWrapper.like("device_id", "%" + alarmQueryDTO.getDeviceId() + "%");
        }
        if (StringUtils.isNotEmpty(alarmQueryDTO.getNickName())) {
            queryWrapper.like("nick_name", "%" + alarmQueryDTO.getNickName() + "%");
        }
        if (StringUtils.isNotEmpty(alarmQueryDTO.getEquipmentNumber())) {
            queryWrapper.like("equipment_number", "%" + alarmQueryDTO.getEquipmentNumber() + "%");
        }
        if (alarmQueryDTO.getBeginTime() != null && alarmQueryDTO.getEndTime() != null) {
            queryWrapper.between("create_time", alarmQueryDTO.getBeginTime(), alarmQueryDTO.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<VideoOpLog> iPage = videoOpLogMapper.selectPage(videoOpLogPage, queryWrapper);
        List<VideoOpLog> videoOpLogList = iPage.getRecords();
        long total = iPage.getTotal();
        if (CollectionUtil.isNotEmpty(videoOpLogList)) {
            videoOpLogList.forEach(v -> {
                VideoOpLogListVO videoOpLogListVO = new VideoOpLogListVO();
                BeanUtils.copyProperties(v, videoOpLogListVO);
                if (videoOpLogListVO.getIntervalMillions() == 0) {
                    videoOpLogListVO.setIntervalMillionsStr("--");
                } else {
                    videoOpLogListVO.setIntervalMillionsStr(DateUtils.getTimeDiffSingle(videoOpLogListVO.getIntervalMillions()));
                }
                videoOpLogListVOList.add(videoOpLogListVO);
            });
        }
        List<VideoOpLogListVO> collect = videoOpLogListVOList.stream().sorted(Comparator.comparing(VideoOpLogListVO::getCreateTime).reversed()).collect(Collectors.toList());
        return JsonResult.success(collect, total);
    }

    @Override
    public List<VideoOpLogListVO> exportAbnormalOfflineAlarmList(AlarmQueryDTO alarmQueryDTO) {
        List<VideoOpLogListVO> videoOpLogListVOList = new ArrayList<>();
//        IPage<VideoOpLog> videoOpLogPage = new Page<>(alarmQueryDTO.getPageNumber(), alarmQueryDTO.getPageSize());
        QueryWrapper<VideoOpLog> queryWrapper = new QueryWrapper<>();
        Integer opType = alarmQueryDTO.getOpType();
        if (opType != null) {
            queryWrapper.eq("op_type", opType);
        } else {
            queryWrapper.in("op_type", VideoOpTypeEnum.DEVICE_LOGOUT_ABNORMAL.getOpType()
                    , VideoOpTypeEnum.CALL_HELP_ALARM.getOpType(), VideoOpTypeEnum.ELECTRON_PATH_ALARM.getOpType());
        }
        if (StringUtils.isNotEmpty(alarmQueryDTO.getDeviceId())) {
            queryWrapper.like("device_id", "%" + alarmQueryDTO.getDeviceId() + "%");
        }
        if (StringUtils.isNotEmpty(alarmQueryDTO.getNickName())) {
            queryWrapper.like("nick_name", "%" + alarmQueryDTO.getNickName() + "%");
        }
        if (StringUtils.isNotEmpty(alarmQueryDTO.getEquipmentNumber())) {
            queryWrapper.like("equipment_number", "%" + alarmQueryDTO.getEquipmentNumber() + "%");
        }
        if (alarmQueryDTO.getBeginTime() != null && alarmQueryDTO.getEndTime() != null) {
            queryWrapper.between("create_time", alarmQueryDTO.getBeginTime(), alarmQueryDTO.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
//        IPage<VideoOpLog> iPage = videoOpLogMapper.selectPage(videoOpLogPage, queryWrapper);
//        List<VideoOpLog> videoOpLogList = iPage.getRecords();
//        long total = iPage.getTotal();
        List<VideoOpLog> videoOpLogList = videoOpLogMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(videoOpLogList)) {
            videoOpLogList.forEach(v -> {
                VideoOpLogListVO videoOpLogListVO = new VideoOpLogListVO();
                BeanUtils.copyProperties(v, videoOpLogListVO);
                videoOpLogListVOList.add(videoOpLogListVO);
            });
        }
        List<VideoOpLogListVO> collect = videoOpLogListVOList.stream().sorted(Comparator.comparing(VideoOpLogListVO::getCreateTime).reversed()).collect(Collectors.toList());
        return collect;
    }


}
